import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;
import nu.xom.ParsingException;
import nu.xom.Serializer;
import nu.xom.ValidityException;
import nu.xom.xslt.XSLException;
import nu.xom.xslt.XSLTransform;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.SimpleXmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.htmlcleaner.XmlSerializer;


//NO FUNCA ESTA VERSION

public class ReadLanacionAndTranslate {

	/**
	 * @param args
	 */
	private static final String ITEMAUTO_XPATH = "//div[@class='itemAutos']";
	public static void main(String[] args) throws XPatherException, MalformedURLException, IOException, ValidityException, ParsingException{
		// TODO Auto-generated method stub
		if (args.length < 2) {
			System.err.println("Usage: java ReadLanacionAndTranslate url stylesheet");
		    return;
		}
		
		// this is where the HtmlCleaner comes in, I initialize it here
        HtmlCleaner cleaner = new HtmlCleaner();
        CleanerProperties props = cleaner.getProperties();
        props.setAllowHtmlInsideAttributes(true);
        props.setAllowMultiWordAttributes(true);
        props.setRecognizeUnicodeChars(true);
        props.setOmitComments(true);
        props.setOmitXmlDeclaration(true);
        
        TagNode node = cleaner.clean(new URL(args[0]));
        //TagNode node = cleaner.clean(args[0]);
		
		Object[] div_nodes = node.evaluateXPath(ITEMAUTO_XPATH);
			
		String result_xml = "<?xml version='1.0'?><root>";
		XmlSerializer serializer = new SimpleXmlSerializer(props);
		for (int i = 0; i < div_nodes.length; i++) {
			result_xml+=serializer.getAsString((TagNode) div_nodes[i]);									
		}
		result_xml+="</root>";
		InputStream is_xml = new ByteArrayInputStream(result_xml.getBytes("UTF-8"));
		
		
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer;
		try {
			transformer = tFactory.newTransformer(new javax.xml.transform.stream.StreamSource(args[1]));
			transformer.transform(new javax.xml.transform.stream.StreamSource(is_xml), new javax.xml.transform.stream.StreamResult( new FileOutputStream("resultado_lanacion.xml")));
		} catch (TransformerConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
//		Builder builder = new Builder();
//		Document doc = builder.build(is_xml);
//		
//		System.setProperty("javax.xml.transform.TransformerFactory",
//		          "org.apache.xalan.processor.TransformerFactoryImpl"); 
//		/*System.setProperty("javax.xml.transform.TransformerFactory", 
//		        "com.icl.saxon.TransformerFactoryImpl");*/
//		     
//		Document stylesheet = builder.build(args[1]);
//		XSLTransform transform;
//		Nodes output = null;
//		Element root = new Element("root");
//		try {			
//			transform = new XSLTransform(stylesheet);			
//			output = transform.transform(doc);
//			System.out.println(stylesheet.toXML());
//			for (int i = 0; i < output.size(); i++) {				
//				root.appendChild(output.get(i).copy());                
//		    }		    
//		} catch (XSLException e) {
//			System.err.print(e);
//			e.printStackTrace();
//		}
//		
//		Document result_doc = new Document(root);
//		
//		try {
//            OutputStream out = new FileOutputStream("resultado_lanacion.xml");
//            out = new BufferedOutputStream(out);
//            Serializer xom_serializer = new Serializer(out, "ISO-8859-1");
//            xom_serializer.write(result_doc);
//        }  
//        catch (IOException ex) {
//            System.err.println("This shouldn't happen for Latin-1!");
//        }
		           
		
		            
      
	}
}
